package edu.fyvtc.javaweb.demo.controller;

import edu.fyvtc.javaweb.demo.common.DemoResult;
import edu.fyvtc.javaweb.demo.common.ResultUtils;
import edu.fyvtc.javaweb.demo.db.DBUtils;
import edu.fyvtc.javaweb.demo.model.House;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value = "/publish")
public class PublishController {

    @RequestMapping("/insert")
    @ResponseBody
    public DemoResult insert(House house) {
        //调用DBUtils的insert方法插入数据，返回插入行数
        int affectedRows = DBUtils.insert(house);
        DemoResult demoResult;
        //如果插入行数大于0，则返回前端操作成功，否则返回操作失败
        if (affectedRows > 0) {
            demoResult = ResultUtils.success();
        } else {
            demoResult = ResultUtils.error();
        }
        return demoResult;
    }
    @RequestMapping("/select")
    @ResponseBody
    public DemoResult select(String id) {
        House house = DBUtils.select(id);
        DemoResult demoResult = ResultUtils.success(house);
        return demoResult;
    }

    @RequestMapping("/update")
    @ResponseBody
    public DemoResult update(House house) {
        DBUtils.update(house);
        DemoResult demoResult = ResultUtils.success();
        return demoResult;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public DemoResult delete(String id) {
        int affectedRows = DBUtils.delete(id);
        DemoResult demoResult;
        if (affectedRows > 0) {
            demoResult = ResultUtils.success();
        } else {
            demoResult = ResultUtils.error();
        }
        return demoResult;
    }

    @RequestMapping("/query")
    @ResponseBody
    public DemoResult query(Double area) {
        List<House> resultList = DBUtils.query(area);
        DemoResult demoResult = ResultUtils.success(resultList);
        return demoResult;
    }

}
